package com.example.projecth.project3;

import java.text.Format;

import com.example.projecth.R;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimerFragment extends Fragment {

	private CountDownTimer timer;
	private Button[] button;
	private TextView textView;
	private NumberPicker[] numPicker;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.timer_fragment, container, false);
		numPicker = new NumberPicker[3];
		numPicker[0] = (NumberPicker) view.findViewById(R.id.hourpicker);
		numPicker[1] = (NumberPicker) view.findViewById(R.id.minpicker);
		numPicker[2] = (NumberPicker) view.findViewById(R.id.secpicker);

//		InputMethodManager imm = 
//			(InputMethodManager)getActivity().getSystemService(
//			      Context.INPUT_METHOD_SERVICE);
		for (int i = 0; i < numPicker.length; i++) {
			String[] nums = new String[60];
			{
				for (int j = 0; j < nums.length; j++)
					nums[j] = Integer.toString(j);
				
				numPicker[i].setMinValue(0);
				numPicker[i].setMaxValue(59);
				numPicker[i].setWrapSelectorWheel(true);
				numPicker[i].setDisplayedValues(nums);
				numPicker[i].setValue(0);
				
				
				
//				imm.hideSoftInputFromWindow(
//					numPicker[i].getWindowToken(), 0);
				for (int x = 0; x < numPicker[i].getChildCount(); x++) {
					View v = numPicker[i].getChildAt(x);
					v.setFocusable(false);
					if (v instanceof EditText)
						((EditText)v).setTextSize(40);
					
						
					
//					imm.hideSoftInputFromWindow(
//							v.getWindowToken(), 0);
				}
				
				
			}
		}
		
		


		textView = (TextView) view.findViewById(R.id.timehour);

		button = new Button[3];
		button[0] = (Button) view.findViewById(R.id.start_timer);
		button[1] = (Button) view.findViewById(R.id.reset_timer);
		button[2] = (Button) view.findViewById(R.id.pick);

		for (int i = 0; i < button.length; i++) {
			button[i].setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					switch (v.getId()) {
					case R.id.start_timer:
												
						if (button[0].getText().toString().equals("START") && timer != null) {
							timer.start();
							button[0].setText("STOP");
							
						} else if (button[0].getText().toString()
								.equals("STOP")) {
							timer.cancel();
							button[0].setText("START");
						}
						break;
					case R.id.reset_timer:
						timer.cancel();
						textView.setText("00:00:00");
						button[0].setText("START");
						break;
					case R.id.pick:
						
						
						if (button[0].getText().toString().equals("START")) {
							textView.setText(String.format("%02d:%02d:%02d",
									numPicker[0].getValue(),
									numPicker[1].getValue(),
									numPicker[2].getValue()));

							if (timer != null) {
								timer.cancel();
							}

							long remainTime = ((3600 * numPicker[0].getValue())
									+ (60 * numPicker[1].getValue()) + numPicker[2]
									.getValue());
							timer = new CountDownTimer(remainTime * 1000, 1000) {

								long hourValue = 0;
								long minValue = 0;
								long secValue = 0;

								@Override
								public void onTick(long millisUntilFinished) {

									hourValue = millisUntilFinished
											/ (3600 * 1000);
									minValue = millisUntilFinished
											/ (60 * 1000) % 60;
									secValue = millisUntilFinished / (1000) % 60;

									textView.setText(String.format(
											"%02d:%02d:%02d", (int) hourValue,
											(int) minValue, (int) secValue));

								}

								@Override
								public void onFinish() {
									textView.setText("00:00:00");
									timer.cancel();

								}

							};
						}

						break;

					}

				}

			});
		}

		return view;

	}

	@Override
	public void onPause() {
		if (timer != null) {
			timer.cancel();
		}
		textView.setText("00:00:00");
		super.onPause();
	}
}
